package mail.gui;

import crypt.VerifyHandler;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import mail.util.Mail;
import org.apache.commons.codec.binary.Base64;

/**
 * Representation class for a single mail.
 * This is shown when a mail has been clicked
 * from the MailReadFrame.
 *
 * @author jonas.lindmark
 */
public class MailFrame extends JInternalFrame implements ActionListener {

    private Mail mail;

    public MailFrame(Mail mail) {
        super(mail.getSubj() + ":" + mail.getFrom(), true, true, true, true);
        this.mail = mail;
        setSize(300, 300);
        setLayout(new BorderLayout());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        JPanel top = new JPanel();
        top.setLayout(new GridLayout(2, 1));
        top.add(new JLabel(mail.getFrom()));
        top.add(new JLabel(mail.getTo()));
        JTextArea body = new JTextArea();
        body.setText(mail.getBody());
        setLayout(new BorderLayout());
        this.add(top, BorderLayout.NORTH);
        this.add(body, BorderLayout.CENTER);

        JButton verify = new JButton("verify");
        this.add(verify, BorderLayout.WEST);
        verify.addActionListener(this);
        verify.setActionCommand("verify");

        validate();
        repaint();
    }

    //Handles action events
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("verify")) {
            verify();
        }
    }
    /**
     * Will attempt to verify _this_ mail
     */
    private void verify() {
        //extract the signature by use of regexp
        Pattern pattern = Pattern.compile("<simplesign>(.*)</simplesign>");
        Matcher matcher = pattern.matcher(mail.getBody());
        boolean matchFound = matcher.find();
        if (matchFound) {
            //if we have a signature we try to verify by asking for
            //a public keypair file to use.
            String sign = matcher.group(1);
            VerifyHandler verifier = new VerifyHandler();
            byte[] data = mail.getBody().replaceAll("<simplesign>(.*)</simplesign>", "").trim().getBytes();
            String file = JOptionPane.showInputDialog("Name of the public file:");
            while (file != null) {
                File f = new File(file);
                if (f.exists()) {
                    boolean res = verifier.verifyByString(data, file, sign);
                    if (res) {
                        JOptionPane.showMessageDialog(null, "Verify: Success");
                    } else {
                        JOptionPane.showMessageDialog(null, "Verify: Unable to verify sender");
                    }
                    break;
                } else {
                    file = JOptionPane.showInputDialog("Name of the public file:");
                }
            }
        } else {
            //no signature found in the mail
            JOptionPane.showMessageDialog(null, "No signature found in mail.");
        }
    }
}
